export class YunioGzip {
  public static async Compress(text: string): Promise<Blob> {
    const cs = new CompressionStream("gzip");
    const writer = cs.writable.getWriter();
    const enc = new TextEncoder();
    writer.write(enc.encode(text));
    writer.close();
    const chunks: Uint8Array<ArrayBuffer>[] = [];
    var reader = cs.readable.getReader();
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      chunks.push(value);
    }
    return new Blob(chunks, { type: 'application/gzip' });
  }
  public static async DeCompress(compress_blob: Blob): Promise<string> {
    const ds = new DecompressionStream("gzip");
    const decompressedStream = compress_blob.stream().pipeThrough(ds);
    const reader = decompressedStream.getReader();
    const decoder = new TextDecoder("utf-8");
    let out = '';
    while (true) {
      const { done, value } = await reader.read();
      if (done) return out;
      out += decoder.decode(value, { stream: true });
    }
  }
}